package org.example.esdemo.repository;

import org.example.esdemo.entity.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.math.BigDecimal;
import java.util.List;

public interface ProductRepository extends ElasticsearchRepository<Product, String> {

    // 根据名称模糊查询
    List<Product> findByName(String name);

    // 根据分类查询
    List<Product> findByCategory(String category);

    // 自定义查询 - 价格区间
    @Query("{\"bool\": {\"must\": [{\"range\": {\"price\": {\"gte\": \"?0\", \"lte\": \"?1\"}}}]}}")
    Page<Product> findByPriceBetween(BigDecimal minPrice, BigDecimal maxPrice, Pageable pageable);

    // 多条件查询 - 名称和分类
    List<Product> findByNameAndCategory(String name, String category);
}